﻿using NetMQ;
using NetMQ.Sockets;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using ZHello.MQ;

namespace ZHello.Net.MQ.netmq
{
    internal class NetMQRepServer : IRepServer
    {
        private ResponseSocket Socket { get; set; }
        public Func<string, string> Respose { get; set; }
        private List<string> BindingAddrs { get; set; }
        public NetMQRepServer(string addr)
        {
            BindingAddrs = new List<string>();
            Socket = new ResponseSocket();
            Socket.Bind(addr);
            BindingAddrs.Add(addr.Trim().ToLower());
            Socket.Poll(TimeSpan.FromMilliseconds(100));
            Socket.ReceiveReady += Socket_ReceiveReady;
        }

        private void Socket_ReceiveReady(object sender, NetMQ.NetMQSocketEventArgs e)
        {
            var msg = Respose?.Invoke(e.Socket.ReceiveFrameString());
            Socket.SendFrame(msg);
        }
    }
}
